<?php namespace Common\Model;
use Hdphp\Model\Model;
//用户模型
class User extends Model
{   
    //指定表名
	protected $table="user";
	//定义自动验证规则
	protected $validate=array(
		 array('username','required','用户名不能为空',3,3),
		 array('password','required','密码不能为空',3,3)
		);
   //验证登陆
	public function vLogin($username,$password)
	{

		//触发自动验证
		if (!$this->create()) return false;
		//判断用户名是否存在
		$data=$this->where("username='{$username}'")->find();
		if (!$data) {
			//错误信息
			$this->error="用户名{$username}不存在";
			return false;
		}
		//密码是否正确
		$password=$this->encrypt($username,$password);
		
		if ($password!=$data['password']) {
			$this->error="密码错误";
			return false;
		}
		
		//存session
		$_SESSION['username']=$username;
		$_SESSION['uid'] = $data['uid'];
    	return true;
	}
	//加密
	public function encrypt($username,$password){
		return md5(md5($username) . md5($password) . 'houdunwang');
	}
}
 ?>